﻿--[[
   @brief    descprtion
   @author   vincentlao
   @version  1.0
   @date     2015-06-11
--]]
group_list_control = 
{    
    init = function()
        base_utility.log("group_list_control init");
        ierd_tgp.tpf_ui.Require("social\\group_list\\view.lua");
        ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");
        ierd_tgp.tpf_ui.Require("social\\group_list\\group_list.lua");
        ierd_tgp.tpf_ui.Require("social\\group_list\\group_guide.lua");
        ierd_tgp.tpf_ui.Require("social\\group_menu\\control.lua");

        comm_center.listen_broadcast("i_broadcast_to_tgp_get_group_list", group_list_control.on_broadcast_to_tgp_get_group_list);
    end;

    get_grouplist_done = function(event_id, json_str)
        base_utility.log(string.format("get_grouplist_done str = %s", json_str));
        group_list_view.get_grouplist_done(event_id, json_str);
        group_list_control.get_recommend_group();
    end;

    show = function(event_id, json_str)
        base_utility.log(string.format("show str = %s", json_str));
        group_list_view.show();
    end;

    on_im_inited = function(event_id, json_str)    
        base_utility.log("[group_list_control]on_im_inited");    
        group_list_view.on_im_inited();
    end;

    on_mini_mode_load_finish = function(event_id, json_str)
        -- local param = base_utility.json_to_table(json_str);
        -- if param.mainframe_mode == 1 then
        --     if group_list_view.is_mini_ui_ ~= true then
        --         group_list_view.is_mini_ui_ = true;
        --         local root_id = group_list_view.uilib_:GetControlIDbyCustomID(CID_MAIN_SOCIAL_FRIEND_CONTAINER_MINIUI);
        --         const_ui_str, str_holder = U8toCUIStr("Ctn_MainFrameTab\\OverlapContainer_Tab\\Ctn_GroupsPnl\\Container_GroupsPnl");
        --         group_list_view.root_id_ = group_list_view.uilib_:GetControlIDbyPath(const_ui_str, root_id);
        --         group_list_view.inited_ = false;
        --         group_list_view.on_mini_mode_load_finish();
        --         if group_list_model.mini_mode_loaded == false then -- 首次mini模式
        --             base_utility.log("[group_list_control]on_mini_mode_load_finish");
        --             group_list_model.mini_mode_loaded = true;
        --             group_list_view.on_mini_mode_load_finish();
        --             --[[if group_list_model.is_get_social_test_info_ == true then
        --                 group_list_view.show_test_image(login_model.social_user_level, login_model.social_invite_code_count);
        --             end]]
        --         end
        --     end
        -- end
    end;

    on_get_my_social_test_info = function(event_id, json_str)
        --[[base_utility.log("[group_list_control]on_get_my_social_test_info");
        if login_model.social_user_level > 0 then
            base_utility.log("[group_list_control]on_get_my_social_test_info have set social_user_level");
            return;
        end

        group_list_model.is_get_social_test_info = true;
        if group_list_model.mini_mode_loaded == true then
            local result_param = base_utility.json_to_table(json_str);
            local social_user_level = 0;
            local social_invite_code_count = 0;
            if result_param["result"] == 0 then
                social_user_level = result_param["level"];
                social_invite_code_count = result_param["invite_code_number"];
            end
            group_list_view.show_test_image(social_user_level, social_invite_code_count);
        end]]

        group_list_model.is_get_social_test_info = true;
        local result_param = base_utility.json_to_table(json_str);
        if result_param["result"] == 0 then
            group_list_model.social_user_level = result_param["level"];
            group_list_model.social_invite_code_count = result_param["invite_code_number"];
        end
        group_list_view.update_ui();
    end;

    on_get_remote_cfg = function(data)
        group_list_model.on_get_remote_cfg(data);
    end;

    get_favor_groupchat_flag = function(data)
        return group_list_model.get_favor_groupchat_flag(data);
    end;

    set_favor_groupchat_flag = function(data)
        group_list_model.set_favor_groupchat_flag(data);
        return {};
    end;

    on_group_new_message_come = function(event_id, json_str)
        base_utility.log(string.format("on_group_new_message_come str = %s", json_str));
        local table_param = base_utility.json_to_table(json_str);
        GroupBannerList:on_group_new_message_come(table_param);
    end;

    on_group_clear_message = function(event_id, json_str)
        base_utility.log(string.format("on_group_clear_message str = %s", json_str));
        local table_param = base_utility.json_to_table(json_str);
        GroupBannerList:on_group_clear_message(table_param.group_id);
    end;

    on_create_group = function(event_id, json_str)
        base_utility.log(string.format("on_create_group str = %s", json_str));
        group_list_view.on_create_group(event_id, json_str);
    end;

    on_join_group = function(event_id, json_str)
        base_utility.log(string.format("on_join_group str = %s", json_str));
        group_list_view.on_join_group(event_id, json_str);
    end;

    on_quit_group = function(event_id, json_str)
        base_utility.log(string.format("on_quit_group str = %s", json_str));
        group_list_view.on_quit_group(event_id, json_str);
    end;

    on_delete_group = function(event_id, json_str)
        base_utility.log(string.format("on_delete_group str = %s", json_str));
        group_list_view.on_delete_group(event_id, json_str);
    end;

    on_recv_group_report = function(event_id, json_str)
        base_utility.log(string.format("on_recv_group_report str = %s", json_str));
        group_list_view.on_recv_group_report(event_id, json_str);
    end;
    
    on_add_group_accepted = function(event_id, json_str)
        base_utility.log(string.format("on_add_group_accepted str = %s", json_str));
        group_list_view.on_add_group_accepted(event_id, json_str);
    end;

    on_add_group_invited = function(event_id, json_str)
        base_utility.log(string.format("on_add_group_invited str = %s", json_str));
        group_list_view.on_add_group_invited(event_id, json_str);
    end;    

    on_live_room_invited_in_group = function(event_id, json_str)
        base_utility.log(string.format("on_live_room_invited_in_group str = %s", json_str));
        group_list_view.on_add_group_invited(event_id, json_str);
    end;

    update_group_notice = function(group_id, notice)
        group_list_model.update_group_notice(group_id, notice);
    end;

    on_get_group_unreaed_message_count = function(event_id, json_str)
        base_utility.log(string.format("on_get_group_unreaed_message_count str = %s", json_str));
        group_list_view.on_get_group_unreaed_message_count(event_id, json_str);
    end;

    on_send_msg_to_group = function(msg)
        GroupBannerList:on_send_msg_to_group(msg);
    end;

    on_get_group_info = function(event_id, json_str)
        base_utility.log(string.format("on_get_group_info str = %s", json_str));
        local table_param = base_utility.json_to_table(json_str);
        if table_param == nil or table_param["result"] ~= 0 then
            base_utility.log("[group_list_control]on_get_group_info error!!");
            return;
        end

        local groups = table_param.groups;
        if groups == nil then
            return;
        end

        for i = 1, #groups do
            local group_old = group_list_model.get_group_info(groups[i].group_id);
            if group_old ~= nil then 
                group_list_model.update_group_info(groups[i]);
            end
        end
    end;

    on_modify_group_info = function(event_id, json_str)
        base_utility.log(string.format("on_modify_group_info str = %s", json_str));
        group_list_view.on_modify_group_info(event_id, json_str);
    end;

    on_group_info_updated = function(event_id, json_str)
        base_utility.log(string.format("on_group_info_updated str = %s", json_str));
        group_list_view.on_group_info_updated(event_id, json_str);
    end;

    get_all_unread_group_id_list = function()
        local groups = {};
        local all_my_group = group_list_model.get_group_list();
        if all_my_group ~= nil then
            for i_group = 1, #all_my_group do
                local group_id = all_my_group[i_group].group_id;
                if group_list_model.get_group_mask_msg_flag(group_id) ~= group_menu_control.FORBID_MSG then --未设置屏蔽闪烁提醒
                    if talk_dialog_control.is_group_session_opened(group_id) == false then
                        local count = group_list_model.get_group_history_unread_msg_count(group_id);
                        if count > 0 then
                            local time = 0;
                            local last_msg = group_list_model.get_last_msg(group_id);
                            if last_msg ~= nil then
                                time = last_msg.time;
                            end
                            local info = {};
                            info.id = group_id;
                            info.is_new = false;
                            info.time = time;
                            info.group_type = group_list_model.get_group_type(group_id);
                            table.insert(groups, info);
                        end
                    end
                end
            end
        end
        return groups;
    end;

    on_mainwnd_show = function ()
        -- data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
        -- if data.is_show == 1 then
        --     if group_list_view.is_mini_ui_ ~= false then
        --         group_list_view.is_mini_ui_ = false;
        --         local root_id = group_list_view.uilib_:GetControlIDbyCustomID(CID_MAIN_SOCIAL_CONTAINER_BIGUI);
        --         const_ui_str, str_holder = U8toCUIStr("Ctn_MainFrameTab\\OverlapContainer_Tab\\Ctn_GroupsPnl\\Container_GroupsPnl");
        --         group_list_view.root_id_ = group_list_view.uilib_:GetControlIDbyPath(const_ui_str, root_id);
        --         group_list_view.inited_ = false;
        --         group_list_model.mini_mode_loaded = true;
        --         group_list_view.on_mini_mode_load_finish();
        --     end
        -- else
        --     --大界面社交主面板没显示
        -- end
    end;

    on_get_total_unread_msg_count = function (args)
        local data = {};
        data['unread_msg_count'] = group_list_model.get_total_unread_msg_count();
        return data;
    end;

    get_recommend_group = function(data_in) --获取推荐群组
        local social_network_model =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        if social_network_model then
            local tpf_ui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpf_ui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_GET_RECOMMENT_GROUP_LIST, "group_list_control.get_recommend_group_rsp");
            social_network_model:get_recommend_group();
        end
    end;

    get_recommend_group_rsp = function(event_id, json_str)
        local tpf_ui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpf_ui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_GET_RECOMMENT_GROUP_LIST, "group_list_control.get_recommend_group_rsp");
        
        local group_tbl = base_utility.json_to_table(json_str);

        base_utility.log("[group_list_control][get_recommend_group_rsp], result:"..group_tbl["result"]..", count:"..group_tbl["count"] )
        if group_tbl and group_tbl["result"] == 0 and group_tbl["count"] > 0 then
            group_list_model.set_recommend_group(group_tbl["groups"]);
        end
    end;

    service_get_group_info = function(param)
        local param_out = {};
        param_out["info"] = group_list_model.get_group_info(param.group_id);
        return param_out;
    end;

    on_broadcast_to_tgp_get_group_list = function(data_in) -- Cross App
        local data_out = {};
        local group_list = {};
        local all_my_group = group_list_model.get_group_list();
        if all_my_group ~= nil then
            for i_group = 1, #all_my_group do
                local group_info = {};
                group_info['group_id'] = all_my_group[i_group].group_id;
                group_info['group_name'] = all_my_group[i_group].name;
                group_info['face_url'] = all_my_group[i_group].face_url;
                group_info['group_type'] = all_my_group[i_group].group_type;

                table.insert(group_list, group_info);
            end
        end

        data_out['group_list'] = group_list;

        comm_center.send_broadcast('i_broadcast_to_app_on_get_group_list', data_out);
    end;
};

group_list_control.init();